package com.ckzp.core.json;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ckzp.core.model.Datas;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 51594 on 2022/7/27.
 */
public class JsonUtil {

    @SuppressWarnings("unchecked")
    public static Map<String, Object> json2Map(String json) {
        try {
            return JSON.parseObject(json, Map.class);
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap();
        }
    }


    @SuppressWarnings("unchecked")
    public static Datas json2Datas(String json) {
        try {
            return JSON.parseObject(json, Datas.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Datas.n(false);
        }
    }


    @SuppressWarnings("unchecked")
    public static Datas json2Datas(JSONObject jo) {
        try {
            return jo.toJavaObject(Datas.class);
        } catch (Exception e) {
            e.printStackTrace();
            return Datas.n(false);
        }
    }

    @SuppressWarnings("unchecked")
    public static Datas json2Datas(Object o) {
        try {
            if (o instanceof JSONObject) {
                return ((JSONObject) o).toJavaObject(Datas.class);
            }
            return Datas.n(false);
        } catch (Exception e) {
            e.printStackTrace();
            return Datas.n(false);
        }
    }


    //.
    public static String toJsonString(Object object) {
        String text = JSON.toJSONString(object);
        return text;
    }


    //.
    public static String toString(Object object) {
        String text = JSON.toJSONString(object);
        return text;
    }
    //.
    public static List json2List(String json, Class clazz) {
        return JSON.parseArray(json, clazz);
    }

    /**
     * @param jsonstr
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T toObject(String jsonstr, Class<T> clazz) {
        return JSON.parseObject(jsonstr, clazz);
    }
}
